//
//  NativeScanChannel.m
//  kq_flutter_widgets
//
//  Created by wtb on 2023/4/7.
//

#import "NativeScanChannel.h"

@implementation NativeScanChannel

+(void)registerWithRegistrar:(NSObject<FlutterPluginRegistrar>*)registrar{
    FlutterMethodChannel *channel = [FlutterMethodChannel methodChannelWithName:@"flutter.keqiang.com/scan" binaryMessenger:[registrar messenger]];
    NativeScanChannel *scan = [[NativeScanChannel alloc] init];
    [registrar addMethodCallDelegate:scan channel:channel];
}

-(void)handleMethodCall:(FlutterMethodCall *)call result:(FlutterResult)result{
    if ([@"startScan" isEqualToString:call.method]){
        NSString *title = call.arguments[@"title"];
        BOOL useAlbum = [call.arguments[@"useAlbum"] boolValue];
        NSArray *types = call.arguments[@"scanTypes"];
        
        NSNumber *color = call.arguments[@"navColor"];
        NSString *note = call.arguments[@"hint"];
        
        // 是否默认开启手电筒
        BOOL turnOnLight = [call.arguments[@"turnOnLight"] boolValue];
        // 是否展示手电筒按钮
        BOOL showLightButton = [call.arguments[@"showLightButton"] boolValue];
        
        NSMutableDictionary *dic = [NSMutableDictionary dictionary];
        
        [dic setValue:[NSNumber numberWithBool:turnOnLight] forKey:@"turnOnLight"];
        [dic setValue:[NSNumber numberWithBool:showLightButton] forKey:@"showLightButton"];
        if(color && ![color isEqual:[NSNull null]]){
            [dic setValue:[NativeScanChannel colorWithHex:[color intValue]] forKey:@"navColor"];
        }
        if(types && ![types isEqual:[NSNull null]] && types.count == 1 && [[NSString stringWithFormat:@"%@",types[0]] isEqualToString:@"0"]){
            ///只支持二维码
            [dic setValue:[NSNumber numberWithBool:YES] forKey:@"notSupportBarcode"];
        }
        if(title && ![title isEqual:[NSNull null]]){
            [dic setValue:title forKey:@"title"];
        }
        if(note && ![note isEqual:[NSNull null]]){
            [dic setValue:note forKey:@"note"];
        }
        [dic setValue:[NSNumber numberWithBool:!useAlbum] forKey:@"notSupportAlbum"];
        void (^scanResultBlock)(NSString *scanResult, NSString *scanType) = ^(NSString *scanResult, NSString *scanType){
            if (scanResult) {
                NSMutableDictionary *dic = [NSMutableDictionary dictionary];
                [dic setValue:@1 forKey:@"code"];
                [dic setValue:scanResult forKey:@"data"];
                [dic setValue:@"" forKey:@"msg"];
                result(dic);
            }else{
                NSMutableDictionary *dic = [NSMutableDictionary dictionary];
                [dic setValue:@0 forKey:@"code"];
                [dic setValue:@"扫码失败" forKey:@"msg"];
                result(dic);
            };
        };
        [dic setValue:scanResultBlock forKey:@"result"];
        // code 为2，flutter不弹出消息
        void (^scanCancelBlock)(void) = ^(){
            NSMutableDictionary *dic = [NSMutableDictionary dictionary];
            [dic setValue:@2 forKey:@"code"];
            [dic setValue:@"取消扫码" forKey:@"msg"];
            result(dic);
        };
        [dic setValue:scanCancelBlock forKey:@"backBlock"];
        
        [NativeScanChannel kqPushWithUrl:@"GFScanViewController" property:dic];
    }
}

+(void)kqPushWithUrl:(NSString *)url property:(NSDictionary *)property{
    Class urlClass= NSClassFromString(url);
    id vc = [[urlClass alloc] init];
    NSArray *keys = property.allKeys;
    for (NSString *key in keys) {
        NSString *value = [property objectForKey:key];
        [vc setValue:value forKey:key];
    }
    if ([NativeScanChannel getNav]) {
        UINavigationController *nav = [NativeScanChannel getNav];
        [nav pushViewController:vc animated:YES];
    }else{
        UIViewController *baseVC = (UIViewController *)vc;
        baseVC.modalPresentationStyle = UIModalPresentationFullScreen;
        [[NativeScanChannel getRootController] presentViewController:baseVC animated:YES completion:nil];
    }
}

+(UIColor *)colorWithHex:(int)hex {
    return [UIColor colorWithRed:((float)((hex & 0xFF0000) >> 16))/255.0
                           green:((float)((hex & 0xFF00) >> 8))/255.0
                            blue:((float)(hex & 0xFF))/255.0
                           alpha:((float)((hex & 0xFF000000) >> 24))/255.0];
}

+(UINavigationController *)getNav{
    Class class= NSClassFromString(@"GFDeviceManager");
    if (class) {
        id obj = [[class alloc] init];
        if ([obj respondsToSelector:@selector(getKqNavigationController)]) {
            UINavigationController *nav = [obj performSelector:@selector(getKqNavigationController)];
            return nav;
        }return nil;
    }
    return nil;
}

+(UIViewController *)getRootController{
    UIWindow *applicationWindow;
    if ([[[UIApplication sharedApplication] delegate] respondsToSelector:@selector(window)]) {
        applicationWindow = [[[UIApplication sharedApplication] delegate] window];
    } else {
        applicationWindow = [[UIApplication sharedApplication] keyWindow];
    }
    UIViewController *flutterVc = applicationWindow.rootViewController;
    return flutterVc;
}
@end
